package cn.ljy.uem.web.action.backstage.school;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.service.SysUserService;
import cn.ljy.authority.web.controller.backstage.BaseBackstageController;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.model.result.ExcelImportResult;
import cn.ljy.common.model.result.ResponseData;
import cn.ljy.common.utils.file.MyFileUpload;
import cn.ljy.common.utils.table.MyTableUtil;
import cn.ljy.uem.model.SchoolMajorGroup;
import cn.ljy.uem.model.query.SchoolMajorGroupQuery;
import cn.ljy.uem.model.query.SchoolMajorQuery;
import cn.ljy.uem.service.SchoolMajorExcelService;
import cn.ljy.uem.service.SchoolMajorGroupService;
import cn.ljy.uem.service.impl.ISchoolMajorGroupExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * @description 专业群的业务层接口
 * @author 王秋林
 * @date 2022-7-18 16:00
 */
@Api(tags = "后台的专业群模块")
@RestController
@RequestMapping(value = "/api/backstage/school/schoolmajorgroup")
public class SchoolMajorGroupController  extends BaseBackstageController<SchoolMajorGroup,SchoolMajorGroupQuery,Integer> {
    @Autowired
    private SchoolMajorGroupService baseService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ISchoolMajorGroupExcelService schoolMajorGroupExcelService;

    @ApiOperation(value = "查询所有专业群")
    @GetMapping("/all")
    @Override
    protected ResponseData query(SchoolMajorGroupQuery query) {
        return super.query(query);
    }

    @ApiOperation(value = "分页查询专业群列表")
    @Override
    public ResponseData query(Integer page, Integer pageSize, SchoolMajorGroupQuery query){
        return super.query(page,pageSize,query);
    }

    @ApiOperation(value = "读取专业群信息")
    @Override
    protected ResponseData get(@PathVariable("id") Integer id) {
        return super.get(id);
    }

    @ApiOperation(value = "批量删除指定的多个专业群")
    @Override
    protected ResponseData deletes(@NotEmpty(message = "操作失败：请选择要移除的记录") Integer[] ids) {
        return super.deletes(ids);
    }


    @ApiOperation(value = "添加专业群")
    @Override
    protected ResponseData add(@Valid @RequestBody SchoolMajorGroup schoolMajorGroup) {
        return super.add(schoolMajorGroup);
    }

    @ApiOperation(value = "编辑专业群")
    @Override
    protected ResponseData edit(@Valid @RequestBody SchoolMajorGroup schoolMajorGroup) {
        return super.edit(schoolMajorGroup);
    }

    @ApiOperation(value = "导入专业群表excel数据")
//    @PreAuthorize("hasAuthority('sys:user:manage:excel:upload')")
    @PostMapping("/excel")
    public ResponseData uploadExcel(MultipartFile file) throws Exception{
        if(file!=null&&!file.isEmpty()){
            String fileName=file.getOriginalFilename();//获取文件名
            String extensionName= FileUtil.extName(fileName);//获取文件扩展名
            //验证表格的格式是否正确
            MyFileUpload.validateExtByDir(extensionName,"table");
            //执行表格导入
            ExcelImportResult result=schoolMajorGroupExcelService.addMajorGroupsFromExcel(file.getInputStream(), UserUtil.getLoginUser());
            //String filePath=uploadFolder+Constant.MATERIAL_EXCEL_UPLOAD_PATH;//获取excel上传后保存的物理路径
            return ResponseData.success(result);
        }else{
            throw new MyWebException("操作失败：请选择上传文件");
        }
    }

    /**
     * 导出表格模板
     */
    @ApiOperation(value = "导出专业模块的excel数据")
//    @PreAuthorize("hasAuthority('sys:user:manage:excelData:download')")
    @GetMapping("/excel")
    public void downloadExcel(HttpServletResponse response, SchoolMajorGroupQuery schoolMajorGroupQuery){
        ExcelWriter writer=schoolMajorGroupExcelService.exportExcel(false,schoolMajorGroupQuery);
        MyTableUtil.downloadExcelByHutool(response,"账户表格",writer);
    }

    /**
     * 导出表格
     */
    @ApiOperation(value = "导出专业的excel模板")
//    @PreAuthorize("hasAuthority('sys:user:manage:excelTemplate:download')")
    @GetMapping("/excel/template")
    public void downloadExcelTemplate(HttpServletResponse response){
        ExcelWriter writer=schoolMajorGroupExcelService.exportExcel(true,null);
        MyTableUtil.downloadExcelByHutool(response,"账户表格模板",writer);
    }


}
